package Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import javax.annotation.Resource;

import org.junit.Test;
import org.springframework.stereotype.Service;

import Dao.UserDao;

@Service(value = "userService")
public class UserServerImp implements UserService {

	@Resource(name = "userDao")
	private UserDao UserDao;

	public UserDao getUserDao() {
		return UserDao;
	}

	public void setUserDao(UserDao userDao) {
		UserDao = userDao;
	}

	public UserServerImp() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public int add(String name, String content, String top) {
		// TODO Auto-generated method stub

		if ("1".equals(top)) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式

			Date date = new Date(System.currentTimeMillis());// （获取启动时间）产生一个当前的毫秒，这个毫秒其实就是自1970年1月1日0时起的毫秒数
			System.out.println(df.format(date));// new Date()为获取当前系统时间
			String fromDate = df.format(date);// 将当前（启动）时间转换为String
			top = fromDate;

		}else{
			top = "0";
		}
		System.out.println("置顶字段"+top);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("content", content);
		map.put("top", top);
		
		int flag =  UserDao.add(map);

		return flag;
	}

	@Test
	public void showtime() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式

		Date date = new Date(System.currentTimeMillis());// （获取启动时间）产生一个当前的毫秒，这个毫秒其实就是自1970年1月1日0时起的毫秒数
		System.out.println(df.format(date));// new Date()为获取当前系统时间
		String fromDate = df.format(date);// 将当前（启动）时间转换为String
		System.out.println(fromDate);
	}

	@Override
	public String show(String name, String content, String top, String page,
			String rows) {
		// TODO Auto-generated method stub
		int PageNow = Integer.parseInt(page);
		int PageSize = Integer.parseInt(rows);
		int from = PageSize*(PageNow-1);
		int to = PageSize;
		
		System.out.println("name "+name);
		System.out.println("content "+content);
		System.out.println("top "+top);
		System.out.println("from "+page);
		System.out.println("to "+rows);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("content", content);
		map.put("top", top);
		map.put("from", from);
		map.put("to", to);
		
		String listUser = UserDao.show(map);
		System.out.println("Service读取数据成功？："+listUser);
		return listUser;
	}

	@Override
	public int destroy(String name) {
		// TODO Auto-generated method stub
		HashMap<String, Object> map = new HashMap<String,Object>();
		map.put("name", name);
		int flag = UserDao.destroy(map);
		return flag;
	}

	@Override
	public String gain(String name) {
		// TODO Auto-generated method stub
		HashMap<String, Object> map = new HashMap<String,Object>();
		map.put("name", name);
		String gainlist = UserDao.gain(map);
		return gainlist;
	}

	@Override
	public int edit(String name, String content, String top) {
		// TODO Auto-generated method stub
		if ("1".equals(top)) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式

			Date date = new Date(System.currentTimeMillis());// （获取启动时间）产生一个当前的毫秒，这个毫秒其实就是自1970年1月1日0时起的毫秒数
			System.out.println(df.format(date));// new Date()为获取当前系统时间
			String fromDate = df.format(date);// 将当前（启动）时间转换为String
			top = fromDate;

		}else{
			top = "0";
		}
		System.out.println("置顶字段"+top);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("content", content);
		map.put("top", top);
		
		int flag =  UserDao.edit(map);

		return flag;
	}

}
